﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.MobileControls;
using System.Web.UI.MobileControls.Adapters;

namespace Shop.WapController.Controls
{
    public class WmlDataPagerAdapter : System.Web.UI.MobileControls.Adapters.WmlControlAdapter
    {
        private DataPager _control;

        public override void OnInit(EventArgs e)
        {
            _control = this.Control as DataPager;
            //load post data, because hiddenvariables change the hidden field name, framework not triigle loadpostdata event
            MobilePage mobilePage = this.Control.MobilePage;
            if (mobilePage.HasHiddenVariables() && mobilePage.HiddenVariables.Contains(this.Control.UniqueID))
            {
                string[] values = mobilePage.HiddenVariables[_control.UniqueID].ToString().Split(new char[] { ',' });
                if (values.Length >= 2)
                {
                    _control.PageIndex = Convert.ToInt32(values[0]);
                    _control.PageCount = Convert.ToInt32(values[1]);
                }
            }
            base.OnInit(e);
        }

        public override void Render(System.Web.UI.MobileControls.Adapters.WmlMobileTextWriter writer)
        {           
            writer.RenderBeginTag(_control.Layout);     //begin pager container                 

            if (_control.Layout == HtmlTextWriterTag.Table)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);    //begin pager tr
                writer.RenderBeginTag(HtmlTextWriterTag.Td);    //begin pager td
            }

            //previous button
            if (_control.PreviousItem != null)
            {
                if (_control.PageIndex > 1)
                {
                    _control.PreviousItem.CanLink = true;
                    _control.PreviousItem.PageIndex = _control.PageIndex - 1;
                }
                else
                {
                    _control.PreviousItem.CanLink = false;
                    _control.PreviousItem.PageIndex = 1;
                }

                this.ComposeNavigationItem(writer, _control.PreviousItem);
            }

            //next button
            if (_control.NextItem != null)
            {
                if (_control.PageIndex < _control.PageCount)
                {
                    _control.NextItem.CanLink = true;
                    _control.NextItem.PageIndex = _control.PageIndex + 1;
                }
                else
                {
                    _control.NextItem.CanLink = false;
                    _control.NextItem.PageIndex = _control.PageCount;
                }

                _control.NextItem.CanLink = _control.PageIndex < _control.PageCount;
                this.ComposeNavigationItem(writer, _control.NextItem);
            }
            
            if (_control.Layout == HtmlTextWriterTag.Table)
            {
                writer.RenderEndTag();      //end td pager navigation
                writer.RenderEndTag();      //end tr pager navigation

                writer.RenderBeginTag(HtmlTextWriterTag.Tr);    //begin tr pager summary
                //pager summary 
                writer.RenderBeginTag(HtmlTextWriterTag.Td);    //begin summary format
                if (string.IsNullOrEmpty(_control.SummaryFormat))
                {
                    _control.SummaryFormat = string.Format(DataPager.DEFAULT_SUMMARY_FORMAT, _control.PageIndex, _control.PageCount, _control.RecordCount);
                }
                else
                {
                    _control.SummaryFormat = string.Format(_control.SummaryFormat, _control.PageIndex, _control.PageCount, _control.RecordCount);
                }

                writer.Write(_control.SummaryFormat);   //writer pager summary
                writer.RenderEndTag();    //end td pager summary
                writer.RenderEndTag();    //begin tr pager summary
            }

            writer.RenderEndTag();      //end pager container            

            //save pageindex and pagecount to client
            _control.MobilePage.HiddenVariables[_control.UniqueID] = _control.PageIndex.ToString() + "," + _control.PageCount.ToString();
        }

        private void ComposeNavigationItem(WmlMobileTextWriter writer, PagerNavigationItem navigation)
        {
            if (navigation.CanLink)
            {
                //save data to client
                //writer.Write(string.Format("<postfield name=\"{0}\" value=\"{1}\" />", this.UniqueID, _pageIndex.ToString() + "," + _pageCount.ToString()));
                //writer.Write("<postfield name=\"__VIEWSTATE\" value=\"/wEXAQUDX19QD2QPBuaD9tz90MyIAgEMU6nkWZWZ0tX+OybC69EimqL+2A==\" />"); ouput viewstate error
                //writer.Write(string.Format("<postfield name=\"__EVENTTARGET\" value=\"{0}\" />", this.UniqueID));
                //writer.Write(string.Format("<postfield name=\"__EVENTARGUMENT\" value=\"{0}\" />", navigation.PageIndex));       

                //way 1 is perfect renderpostback is achor and has go card __pcb1
                //before renderbeginpostback framework will output a break markup, why?    protected virtual void EnsureLayout(), can call RenderText                
                writer.RenderBeginPostBack(navigation.Text, true, false);
                //call rendertext void break.  this.EnsureLayout();
                writer.RenderText(" " + navigation.Text + " ", false, false);
                //includevariables must is true, otherwise aspnet mobile will output than more one post card.
                writer.RenderEndPostBack(_control.UniqueID, navigation.PageIndex.ToString(), WmlPostFieldType.Raw, true, false);
                
                
                //way 2
                //writer.Write("<anchor title=\"" + navigation.Text + "\"> " + navigation.Text);
                //writer.RenderGoAction(_control.UniqueID, navigation.PageIndex.ToString(), WmlPostFieldType.Raw, false);
                //writer.Write("</anchor>");    
                        

                //way 3
                //use aspnet mobile default post card's postfield, hardcode the postfield variable name, but i can't make sure the post card(#__pcb1) had output.
                //writer.Write("<anchor title=\"" + navigation.Text + "\"> " + navigation.Text);
                //writer.Write(string.Format(" <go href=\"{0}\" method=\"post\">", "#__pbc1"));
                //writer.WritePostField("mcsvt", _control.UniqueID);
                //writer.WritePostField("mcsva", navigation.PageIndex.ToString());
                //writer.Write("</go></anchor>");                
            }
            else
            {
                writer.RenderText(" " + navigation.Text + " ", false, false);
            }            
        }

    }
}
